import 'dart:async';

import 'package:flutter/material.dart';
import 'package:get/get.dart';

class CountDownDialog extends StatefulWidget {
  String title;
  late Function? back;

  CountDownDialog({Key? key, this.title: "", this.back}) : super(key: key);

  @override
  _CountDownDialogState createState() => _CountDownDialogState();
}

class _CountDownDialogState extends State<CountDownDialog> {
  late Timer timer;
  RxInt count = 3.obs;

  RxBool canSubmit = false.obs;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    timer = Timer.periodic(Duration(seconds: 1), (_) {
      if (count.value == 1) {
        canSubmit.value = true;
        setState(() {});
        timer.cancel();
      } else {
        count--;
        setState(() {});
      }
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    timer.cancel();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Container(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            const SizedBox(
              height: 20,
            ),
            Container(
              alignment: Alignment.center,
              child: Text(
                widget.title,
                style: TextStyle(
                    fontSize: 16, height: 1.5, color: Color(0xFF666666)),
                textAlign: TextAlign.center,
              ),
              padding: EdgeInsets.symmetric(horizontal: 14),
              margin: EdgeInsets.only(bottom: 20),
            ),
            Container(
              height: 43,
              decoration: BoxDecoration(
                  border: Border(
                      top: BorderSide(width: 1, color: Color(0xFFEEEEEE)))),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  Expanded(
                    child: InkWell(
                      onTap: () {
                        Get.back();
                      },
                      child: Text(
                        "取消",
                        style: TextStyle(fontSize: 16, color: Colors.grey),
                        textAlign: TextAlign.center,
                      ),
                    ),
                  ),
                  Container(
                    height: 43,
                    width: 1,
                    color: Color(0xFFEEEEEE),
                  ),
                  Expanded(
                      child: InkWell(
                    onTap: () {
                      if (canSubmit.value) {
                        Get.back();
                        widget.back!();
                      }
                    },
                    child: Text(
                      "${canSubmit.value ? "知道了" : "(${count.value}s)"}",
                      style: TextStyle(fontSize: 16, color: Color(0xFFFEBA07)),
                      textAlign: TextAlign.center,
                    ),
                  ))
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}
